home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / MacInternalFrameTitlePane.java < prev    next >
Text File  |  1998-06-30  |  12KB  |  392 lines

  1. /*
  2.  * @(#)MacInternalFrameTitlePane.java    1.8 98/04/10
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21.  
  22. package com.sun.java.swing.plaf.mac;
  23.  
  24. import java.awt.*;
  25. import java.awt.event.*;
  26. import com.sun.java.swing.*;
  27. import com.sun.java.swing.border.*;
  28. import com.sun.java.swing.event.InternalFrameEvent;
  29. import java.util.EventListener;
  30. import java.beans.PropertyChangeListener;
  31. import java.beans.PropertyChangeEvent;
  32. import java.beans.VetoableChangeListener;
  33. import java.beans.PropertyVetoException;
  34.  
  35. /**
  36.  * Package private class that manages a Mac title bar
  37.  *
  38.  * @version @(#)MacInternalFrameTitlePane.java    1.0 11/24/97
  39.  * @author Symantec
  40.  * @author David Bustin
  41.  */
  42.  
  43. class MacInternalFrameTitlePane 
  44.     extends JComponent implements LayoutManager, ActionListener, PropertyChangeListener 
  45. {
  46.     CloseButton closeButton;
  47.     MinimizeButton minimizeButton;
  48.     ZoomButton zoomButton;
  49.     Title title;
  50.     JInternalFrame iFrame;
  51.     Color color;
  52.     Color highlight;
  53.     Color shadow;
  54.  
  55.     static final Font defaultTitleFont = new Font("Chicago", Font.PLAIN, 12);
  56.     
  57.     protected static Color GSWColor = UIManager.getColor("GrayscaleAppearanceW");
  58.     protected static Color GS2Color = UIManager.getColor("GrayscaleAppearance2");
  59.     protected static Color GS3Color = UIManager.getColor("GrayscaleAppearance3");
  60.     //protected static Color GS8Color = UIManager.getColor("GrayscaleAppearance8");
  61.  
  62.     // The width and height of a title pane button
  63.     public final static int BUTTON_SIZE = 17;    // 13 + INDENT
  64.     public final static int INDENT = 4;
  65.     
  66.     public MacInternalFrameTitlePane(JInternalFrame f) {
  67.     iFrame = f;
  68.     
  69.     setPreferredSize(new Dimension(100, BUTTON_SIZE));
  70.  
  71.     closeButton = new CloseButton();
  72.     closeButton.addActionListener(this);
  73.     closeButton.setActionCommand("Close");
  74.  
  75.     minimizeButton = new MinimizeButton();
  76.     minimizeButton.addActionListener(this);
  77.     minimizeButton.setActionCommand("Iconify");
  78.  
  79.     zoomButton = new ZoomButton();
  80.     zoomButton.addActionListener(this);
  81.     zoomButton.setActionCommand("Maximize");
  82.  
  83.     title = new Title(iFrame.getTitle());
  84.     title.setFont(defaultTitleFont);
  85.  
  86.     setLayout(this);
  87.  
  88.     add(closeButton);
  89.     add(title);
  90.     add(minimizeButton);
  91.     add(zoomButton);
  92.  
  93.     // Make sure these are ok to leave on?
  94.     iFrame.addPropertyChangeListener(this);
  95.     }
  96.  
  97.     void setColors(Color c, Color h, Color s) {
  98.         color = c;
  99.         highlight = h;
  100.         shadow = s;
  101.     }
  102.  
  103.     public void actionPerformed(ActionEvent e) {
  104.         try {
  105.             if ("Close".equals(e.getActionCommand()) && iFrame.isClosable()) {
  106.                 postClosingEvent(iFrame); 
  107.             } else if ("Iconify".equals(e.getActionCommand()) && 
  108.                      iFrame.isIconifiable()) {
  109.                 if (!iFrame.isIcon()) {
  110.                     iFrame.setIcon(true); 
  111.                 } else {
  112.                     iFrame.setIcon(false); 
  113.                 }
  114.             } else if ("Minimize".equals(e.getActionCommand()) && 
  115.                        iFrame.isMaximizable()) {
  116.                 iFrame.setIcon(true);
  117.             } else if ("Maximize".equals(e.getActionCommand()) && 
  118.                        iFrame.isMaximizable()) {
  119.                 if(!iFrame.isMaximum()) {
  120.                     iFrame.setMaximum(true);
  121.                 } else {
  122.                     iFrame.setMaximum(false);
  123.                 }
  124.             }
  125.         } catch (PropertyVetoException e0) { }
  126.  
  127.     }
  128.  
  129.     public void propertyChange(PropertyChangeEvent evt) {
  130.     String prop = (String)evt.getPropertyName();
  131.     JInternalFrame f = (JInternalFrame)evt.getSource();
  132.     boolean value = false;
  133.     if(JInternalFrame.IS_SELECTED_PROPERTY.equals(prop)) {
  134.         repaint();
  135.     } else if(JInternalFrame.IS_MAXIMUM_PROPERTY.equals(prop)) {
  136.         value = ((Boolean)evt.getNewValue()).booleanValue();
  137.     } else if(JInternalFrame.IS_ICON_PROPERTY.equals(prop)) {
  138.         value = ((Boolean)evt.getNewValue()).booleanValue();
  139.     } 
  140.     }
  141.  
  142.     public void addLayoutComponent(String name, Component c) {}
  143.     public void removeLayoutComponent(Component c) {}    
  144.     public Dimension preferredLayoutSize(Container c)  {
  145.     return new Dimension(c.getSize().width, BUTTON_SIZE);
  146.     }
  147.     
  148.     public Dimension minimumLayoutSize(Container c) {
  149.     return new Dimension(c.getSize().width, BUTTON_SIZE);
  150.     }
  151.  
  152.     public void paint(Graphics g) {
  153.         Dimension d = getSize();
  154.         if (iFrame.isSelected())
  155.                 g.setColor(GS3Color);
  156.         else
  157.                 g.setColor(GS2Color);
  158.         g.fillRect(0,0,d.width-1,d.height-1);
  159.         super.paint(g);
  160.     }    
  161.  
  162.     public void layoutContainer(Container c) {
  163.     int w = getWidth();
  164.     int x = w - BUTTON_SIZE + INDENT;
  165.     if (iFrame.isClosable()) 
  166.         {
  167.         closeButton.setBounds(0, 0, BUTTON_SIZE, BUTTON_SIZE);
  168.         }
  169.  
  170.     if (iFrame.isIconifiable()) 
  171.         {
  172.         minimizeButton.setBounds(x, 0, BUTTON_SIZE, BUTTON_SIZE);
  173.         x -= BUTTON_SIZE;
  174.         } 
  175.     else if (minimizeButton.getParent() != null)
  176.         {
  177.         minimizeButton.getParent().remove(minimizeButton);
  178.         }
  179.  
  180.     if (iFrame.isMaximizable())
  181.         {
  182.         zoomButton.setBounds(x, 0, BUTTON_SIZE, BUTTON_SIZE);
  183.         x -= BUTTON_SIZE;
  184.         } 
  185.     else 
  186.         if(zoomButton.getParent() != null)
  187.         {
  188.         zoomButton.getParent().remove(zoomButton);
  189.         }
  190.         
  191.     if (iFrame.isClosable())
  192.         title.setBounds(BUTTON_SIZE, 0, x-INDENT, BUTTON_SIZE);
  193.     else
  194.         title.setBounds(0, 0, x+BUTTON_SIZE-INDENT, BUTTON_SIZE);
  195.  
  196.     }
  197.  
  198.     /**
  199.      * Post a WINDOW_CLOSING-like event to the frame, so that it can
  200.      * be treated like a regular Frame.
  201.      */
  202.     void postClosingEvent(JInternalFrame frame) {
  203.         InternalFrameEvent e = new InternalFrameEvent(
  204.             frame, InternalFrameEvent.INTERNAL_FRAME_CLOSING);
  205.         // Try posting event, unless there's a SecurityManager.
  206.         if (JInternalFrame.class.getClassLoader() == null) {
  207.             try {
  208.                 Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(e);
  209.                 return;
  210.             } catch (SecurityException se) {
  211.                 // Use dispatchEvent instead.
  212.             }
  213.         }
  214.         frame.dispatchEvent(e);
  215.     }
  216.  
  217.     static Dimension buttonDimension = new Dimension(BUTTON_SIZE, BUTTON_SIZE);
  218.  
  219.     private abstract class FrameButton extends JButton {
  220.  
  221.         FrameButton() {
  222.             super();
  223.             setFocusPainted(false);     
  224.             setBorderPainted(false);
  225.         }
  226.  
  227.         public boolean isFocusTraversable() { 
  228.             return false; 
  229.         }
  230.  
  231.         public void requestFocus() {
  232.             // ignore request.
  233.         }
  234.  
  235.         public Dimension getMinimumSize() {
  236.             return buttonDimension;
  237.         }
  238.  
  239.         public Dimension getPreferredSize() {
  240.             return buttonDimension;
  241.         }
  242.  
  243.         public void paint(Graphics g) {
  244.            
  245.            if (iFrame.isSelected())
  246.                 {
  247.                 Dimension d = getSize();
  248.                 int maxX = d.width;
  249.                 int maxY = d.height - 5;
  250.  
  251.                 // draw background
  252.                 g.setColor(GS3Color);
  253.                 g.fillRect(0,0,maxX,d.height-1);
  254.                 
  255.                 /* Disabled title bar shading to hightlight difference from
  256.                    normal Mac style frames */
  257.                 /*
  258.                 g.setColor(GSWColor);
  259.                 for (int y = 0; y < maxY; y += 2)
  260.                     g.drawLine(0,y,d.width - 2,y);
  261.                 
  262.                 g.setColor(GS8Color);
  263.                 for (int y = 1; y < maxY; y += 2)
  264.                     g.drawLine(1,y,d.width - 1,y);
  265.                 */
  266.                 }
  267.             else
  268.                 {
  269.                  g.setColor(GS2Color);
  270.  
  271.                 Dimension d = getSize();
  272.                 g.fillRect(0,0,d.width,d.height-1);
  273.                 }
  274.         }
  275.     }
  276.     
  277.     private class MinimizeButton extends FrameButton {
  278.         private Icon minimizeEnabledIcon = UIManager.getIcon("Window.minimizeEnabled");
  279.         private Icon minimizePressedIcon = UIManager.getIcon("Window.minimizePressed");
  280.         public void paint(Graphics g) {
  281.             if (iFrame.isSelected())
  282.                 {
  283.                 if (getModel().isPressed())
  284.                     minimizePressedIcon.paintIcon(this, g, 0, 0);
  285.                 else
  286.                     minimizeEnabledIcon.paintIcon(this, g, 0, 0);
  287.                 }
  288.             else
  289.                 super.paint(g);
  290.        }
  291.     }
  292.  
  293.     private class ZoomButton extends FrameButton {
  294.         private Icon zoomEnabledIcon = UIManager.getIcon("Window.zoomEnabled");
  295.         private Icon zoomPressedIcon = UIManager.getIcon("Window.zoomPressed");
  296.         public void paint(Graphics g) {
  297.             if (iFrame.isSelected())
  298.                 {
  299.                 if (getModel().isPressed())
  300.                     zoomPressedIcon.paintIcon(this, g, 0, 0);
  301.                 else
  302.                     zoomEnabledIcon.paintIcon(this, g, 0, 0);
  303.                 }
  304.             else
  305.                 super.paint(g);
  306.         }
  307.     }
  308.  
  309.     private class CloseButton extends FrameButton {
  310.         private Icon closeEnabledIcon = UIManager.getIcon("Window.closeEnabled");
  311.         private Icon closePressedIcon = UIManager.getIcon("Window.closePressed");
  312.         public boolean isFocusTraversable() { return false; }
  313.         public void requestFocus() {}
  314.  
  315.         public void paint(Graphics g) {
  316.            if (iFrame.isSelected())
  317.                 {
  318.                 if (getModel().isPressed())
  319.                     closePressedIcon.paintIcon(this, g, 0, 0);
  320.                 else
  321.                     closeEnabledIcon.paintIcon(this, g, 0, 0);
  322.                 }
  323.             else
  324.                 super.paint(g);
  325.         }
  326.     }
  327.  
  328.     private class Title extends FrameButton {
  329.         Title(String title) {
  330.             super();
  331.             setText(title);
  332.             setHorizontalAlignment(SwingConstants.CENTER);
  333.             
  334.             setBorder(BorderFactory.createBevelBorder(
  335.                 BevelBorder.RAISED,
  336.                 UIManager.getColor("activeCaptionBorder"),
  337.                 UIManager.getColor("inactiveCaptionBorder")));
  338.  
  339.             // Forward mouse events to titlebar for moves.
  340.             addMouseMotionListener(new MouseMotionListener() {
  341.                 public void mouseDragged(MouseEvent e) {
  342.                     forwardEventToParent(e);
  343.                 }
  344.                 public void mouseMoved(MouseEvent e) {
  345.                     forwardEventToParent(e);
  346.                 }
  347.             });
  348.             addMouseListener(new MouseListener() {
  349.                 public void mouseClicked(MouseEvent e) {
  350.                     forwardEventToParent(e);
  351.                 }
  352.                 public void mousePressed(MouseEvent e) {
  353.                     forwardEventToParent(e);
  354.                 }
  355.                 public void mouseReleased(MouseEvent e) {
  356.                     forwardEventToParent(e);
  357.                 }
  358.                 public void mouseEntered(MouseEvent e) {
  359.                     forwardEventToParent(e);
  360.                 }
  361.                 public void mouseExited(MouseEvent e) {
  362.                     forwardEventToParent(e);
  363.                 }
  364.             });
  365.         }
  366.  
  367.         void forwardEventToParent(MouseEvent e) {
  368.             getParent().dispatchEvent(new MouseEvent(
  369.                 getParent(), e.getID(), e.getWhen(), e.getModifiers(),
  370.                 e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger()));
  371.         }
  372.  
  373.         public void paint(Graphics g) {
  374.             Color background;
  375.             super.paint(g);
  376.             if (iFrame.isSelected()) {
  377.                 background = GS3Color;
  378.                 g.setColor(UIManager.getColor("activeCaptionText"));
  379.             } else {
  380.                 background = GS2Color;
  381.                g.setColor(UIManager.getColor("inactiveCaptionText"));
  382.             }
  383.             Dimension d = getSize();
  384.             MacGraphicsUtils.drawStringInRect(g, iFrame.getTitle(),
  385.                                                 0, 0, d.width, d.height,
  386.                                                 SwingConstants.CENTER,
  387.                                                 background);
  388.         }
  389.     }
  390.  
  391. }    /// End Title Pane Class
  392.